package com.java1902.grademanager;

import java.util.ArrayList;
import java.util.List;

/**
 * 班级管理类
 */
public class GradeManager {
	/** 添加班级命令 */
	private static final int GRADE_ADD = 1;
	/** 显示班级命令 */
	private static final int GRADE_SHOW = 2;
	/** 修改班级命令 */
	private static final int GRADE_UPDATE = 3;
	private static final int GRADE_DELETE = 4;
	/** 退出命令 */
	private static final int GRADE_EXIT = 5;

	/** 存储班级容器 */
	private List<Grade> gradeLists = new ArrayList<>();

	/**
	 * 启动班级管理系统
	 */
	public void startGradeManger() {
		System.out.println("========欢迎进入班级管理系统=========");
		boolean isExit = true;
		while (isExit) {
			System.out.println("【1】添加班级  【2】显示班级 【3】修改 【4】删除【5】退出");
			int command = Utils.inputInt();
			switch (command) {
			case GRADE_ADD:
				addGrade();
				break;
			case GRADE_SHOW:
				show();
				break;
			case GRADE_UPDATE:
				updateGrade();
				break;
			case GRADE_DELETE:
				deleteGrade();
				break;
			case GRADE_EXIT:
				System.out.println("===退出成功!===");
				isExit = false;
				break;
			}
		}
	}

	/**
	 * 添加班级
	 */
	public void addGrade() {
		System.out.println("请输入班级名 :");
		String name = Utils.inputString();
		System.out.println("请输入班编号 :");
		String number = Utils.inputString();

		// 查检添加班级是否已经存在
		Grade oldGrade = findGradeById(number);
		if (oldGrade != null) {
			System.out.println("编号为" + number + "的班级已经存在!");
			return;
		}

		Grade grade = new Grade(name, number);
		gradeLists.add(grade);
	}

	/**
	 * 显示所有班级
	 */
	public void show() {
		if (gradeLists.size() == 0) {
			System.out.println("没有班级!");
			return;
		}

		System.out.println("编号\t班级名称");
		for (Grade grade : gradeLists) {
			System.out.println(grade);
		}
	}

	/**
	 * 根据班级编号修改班级名称 思想: 录入编号->找到班级->修改班级
	 */
	public void updateGrade() {
		Grade grade = findGradeById();
		System.out.println("输入新班级名称 :");
		String name = Utils.inputString();
		grade.setName(name);
		System.out.println("修改成功!");
	}

	/**
	 * 根据编号查找班级
	 */
	public Grade findGradeById() {
		System.out.println("请输入班级编号 :");
		String number = Utils.inputString();
		Grade grade = findGradeById(number);
		return grade;
	}

	/**
	 * 根据编号查找班级
	 * 
	 * @param id
	 * @return
	 */
	public Grade findGradeById(String num) {
		Grade grade = null;
		// 2. 根据编号查找班级： 思路:遍历 班级列表
		int size = gradeLists.size();
		for (int i = 0; i < size; i++) {
			Grade grade1 = gradeLists.get(i); // 从班级容器获取下标为i的班级
			String id = grade1.getNumber(); // 获取班级容器中班级编号
			if (id.equals(num)) {
				grade = grade1;
				break;
			}
		}
		return grade;
	}

	/**
	 * 根据编号删除班级
	 * 
	 */
	public void deleteGrade() {
		System.out.println("请输入班编号 :");
		String number = Utils.inputString();
		Grade Grade = findGradeById(number);
		gradeLists.remove(Grade);

	}

}
